package uia.alumni.group;

import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import uia.alumni.web.Page;
import uia.alumni.data.Group;

import static uia.alumni.form.FieldType.*;
import static uia.alumni.web.Constants.*;


/**
 * Servlet that shows a list of all the groups.
 * @author Ohm
 * @version v1 date:10.10.2009 by Ohm
 */
public class ListGroups extends Page{
    public static final String SERVLET_NAME = "group.list";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;

    public ListGroups(){
        super(null);
    }

        public void writeBody(PrintWriter out, HttpServletRequest request){
            EntityManager em = emf.createEntityManager();


            try{
                Query q = em.createNamedQuery("Group.findAll");
                List<Group> groups = q.getResultList();
                for (Group group : groups) {
                out.println("Groups:" + group.getName() + "<br/>");
            }
            out.println("");


            em.close();
            }catch(Exception e){


            }
        }


}

